package hsl.study.controller;


import hsl.study.config.MySQLChatMemory;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/7/31 17:58
 * @Version: 1.0
 */


@RestController
@RequestMapping("/mySqlChatMemory")
public class MySQLChatMemoryController {

    @Resource(name = "deepseekChatClient")
    private ChatClient deepseekChatClient;
    @Resource
    private MySQLChatMemory mySQLChatMemory;

    //http://localhost:6007/mySqlChatMemory/chat?question=1加7等于多少直接给答案&sessionId=2
    @GetMapping("/chat1")
    public Flux<String> chat(String question, String sessionId) {
        // 上下文管理对象
        MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor.builder(mySQLChatMemory)//初始化构建器，传入 ChatMemory 实例
                .conversationId(sessionId)//设置会话 ID（可选）
                .scheduler(Schedulers.boundedElastic())//设置调度器（用于异步写入内存/持久化）
                .build();

        return deepseekChatClient.prompt()
                .user(question)
                .advisors(messageChatMemoryAdvisor)  // 关键代码：大模型注入记忆功能
                .stream()
                .content();
    }
}
